package com.zn.util;

import com.aliyun.oss.OSSClient;
import com.zn.bo.WaterMarkBO;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

/**
 * @author YinJiaWei
 * @version 2019/1/9 15:24
 */
public class WatermarkPic {
    private final static String CSSCOLOR = "#AAAAAA";
    private final static float ALPHA = 0.6f;
    private final static int IMGHEIGHT=170;
    private final static Integer DEGREE=-5;

    public static void main(String[] args) {
        WaterMarkBO waterMarkBO=new WaterMarkBO();
        waterMarkBO.setFileUrl("http://zn-50etf-test.oss-cn-shanghai.aliyuncs.com/Watermark/25-copy-2.png");
        waterMarkBO.setTitleName("《免责条款》");
        waterMarkBO.setMarkContent("生效日期:2018-12-26");
    }

    public static void createMark(WaterMarkBO waterMarkBO,OSSUtil ossUtil,String fileName,String imgFolder) {
        BufferedImage bimage = buildGraph2d(waterMarkBO);
        outputPic(bimage, ossUtil,fileName,imgFolder);
    }

    /**
     * 构造2d画笔
     */
    private static BufferedImage buildGraph2d(WaterMarkBO waterMarkBO){
        Color markContentColor = Color.gray;
        URL url;
        try {
            url = new URL(waterMarkBO.getFileUrl());
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        ImageIcon imgIcon = new ImageIcon(url);
        Image theImg = imgIcon.getImage();
        //Image可以获得 输入图片的信息
        int width = theImg.getWidth(null);
        int height = theImg.getHeight(null);
        //为画出图片的大小
        BufferedImage bimage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
        //2d 画笔
        Graphics2D g = bimage.createGraphics();
        g.setColor(markContentColor);
        g.setBackground(Color.white);
        //画出图片-----------------------------------
        g.drawImage(theImg, 0, 0, null);
        //--------对要显示的文字进行处理--------------
        AttributedCharacterIterator title = buildFont(waterMarkBO.getTitleName());
        AttributedCharacterIterator content = buildFont(waterMarkBO.getMarkContent());
        Color color = Color.decode(CSSCOLOR);
        g.setColor(color);
        if (null != DEGREE) {
            // 设置水印旋转
            g.rotate(Math.toRadians(DEGREE),
                    (double) bimage.getWidth() / 2, (double) bimage
                            .getHeight() / 2);
        }
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                ALPHA));
        int line=height/IMGHEIGHT;
        /**指定行数**/
        for(int i=0;i<line;i++){
            /**一行多少列**/
            for(int j = 0;j<3;j++){
                int x=(width - (width*j/2)-320);
                int y=(height - (height*i/line));
//                System.err.println("width--->"+width+",height---->"+height+",x--->"+x+",y-------->"+y);
                g.drawString(content, x, y );
                g.drawString(title, x,y-30 );

            }
//            System.err.println("------------------------------------->");
        }
        //添加水印的文字和设置水印文字出现的内容 ----位置
        g.dispose();//画笔结束
        return bimage;
    }

    /**
     * 生成图片写入oss中
     * @param bimage
     * @param ossUtil
     * @param imgFolder
     */
    private static void outputPic(BufferedImage bimage,OSSUtil ossUtil,String fileName,String imgFolder) {
        ByteArrayOutputStream out = null;
        try {
            out = new ByteArrayOutputStream();
            ImageIO.write(bimage, "png", out);
            InputStream input = new ByteArrayInputStream(out.toByteArray());
            OSSClient ossClient = ossUtil.getOSSClient();
            //开始上传水印图片
            ossUtil.uploadInputStreamObject2OSS(ossClient, input, fileName, imgFolder);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 设置字体属性
     * @param markContent
     * @return
     */
    private static AttributedCharacterIterator buildFont(String markContent) {
        AttributedString ats = new AttributedString(markContent);
        Font f = new Font("微软雅黑", Font.PLAIN, 23);
        ats.addAttribute(TextAttribute.FONT, f, 0, markContent.length());
        AttributedCharacterIterator iter = ats.getIterator();
        return iter;
    }

}
